﻿using System;
using FileHelpers;

namespace ExamplesFx
{
    //-> Name: Read Delimited File
    //-> Description: How to read a Delimited File
    //-> AutoRun: true

    public class ReadFileDelimited
        : ExampleBase
    {
      
        //-> If you have a source file like this, separated by a |:
      
        //-> FileIn:Input.txt
/*10248|VINET|04071996|32.38
10249|TOMSP|05071996|11.61
10250|HANAS|08071996|65.83
10251|VICTE|08071996|41.34*/
        //-> /FileIn


        //-> You first declare a Record Mapping Class:

        //-> File:RecordClass.cs
        [DelimitedRecord("|")]
        public class Orders
        {
            public int OrderID;

            public string CustomerID;

            [FieldConverter(ConverterKind.Date, "ddMMyyyy")]
            public DateTime OrderDate;

            [FieldConverter(ConverterKind.Decimal, ".")] // The decimal separator is .
            public decimal Freight;
        }

        //-> /File

        //->  Instantiate a FileHelperEngine and read or write files:

        public override void Run()
        {
            //-> File:Example.cs
            var engine = new FileHelperEngine<Orders>();
            var records = engine.ReadFile("Input.txt");

            foreach (var record in records)
            {
                Console.WriteLine(record.CustomerID);
                Console.WriteLine(record.OrderDate.ToString("dd/MM/yyyy"));
                Console.WriteLine(record.Freight);
            }
            //-> /File
        }

        /*->Html: <p>Now you have an Orders array named <span class="code">res</span> where
                  every item in the array is an Order object. If you want to access one of the fields
                  let the Visual Studio IntelliSense bring up the field names for you.</p>
                  <div class="indent">
                     <img alt="Visual studio intellisense" src="" />
                  </div>
         */
    }
}